package com.shengde.jinwawaforleader.model.myJob.classList;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.base.CommonBaseAdapter;
import com.shengde.jinwawaforleader.base.ViewHolder;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.model.myJob.instance_message.Constant;
import com.shengde.jinwawaforleader.model.personalCenter.activity.EditBabyNameActivity;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProGetIdCardInfo;
import com.shengde.jinwawaforleader.network.protocolbean.ProShowClickGood;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 绑定id卡
 * Created by Administrator on 2016/12/1 0001.
 */
public class BindingIdCardActivity extends BaseActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
	private static final String TAG = "BindingIdCardActivity";
	private static final int FINSH = 1;
	private TextView tv_back;
	private TextView tv_title;
	private TextView fill_add;
	private ListView lv_binding_id_card;
	private ArrayList<ProGetIdCardInfo.CarListBean> list;
	private String type;
	private String id;

	private CommonBaseAdapter commonBaseAdapter;
	private List<ProGetIdCardInfo.CarListBean> carListBeen;
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case FINSH:
					if (commonBaseAdapter == null) {
						commonBaseAdapter = new CommonBaseAdapter(BindingIdCardActivity.this, list, R.layout.binding_id_item) {
							@Override
							public void convert(ViewHolder holder, int position) {
								holder.setText(R.id.tv_id_card, getString(R.string.tv_id_card) + (position + 1));
								holder.setText(R.id.tv_name, list.get(position).getRegisterID());

							}
						};
						lv_binding_id_card.setAdapter(commonBaseAdapter);
					} else {
						Log.i(TAG, "handleMessage: size = " + list.size());
						Log.i(TAG, "handleMessage: commonBaseAdapter = " + commonBaseAdapter.getCount());
						commonBaseAdapter.notifyDataSetChanged();
					}
					break;
			}
		}
	};

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.binding_id_card_activity);
	}

	@Override
	public void initView() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		fill_add = (TextView) findViewById(R.id.fill_add);
		lv_binding_id_card = (ListView) findViewById(R.id.lv_binding_id_card);

	}

	@Override
	public void initData() {
		type = getIntent().getStringExtra("type");
		distinguishType();

		list = new ArrayList();
		fill_add.setText(getString(R.string.add));
		tv_title.setText(getString(R.string.bingding_id_card));

	}

	/**
	 * 区分 老师id 还是学生id
	 */
	private void distinguishType() {
		if ("1".equals(type)) {
			String mTeacherId = getIntent().getStringExtra("teacherid");
			id = mTeacherId;
		} else if ("2".equals(type)) {
			String mStudentId = getIntent().getStringExtra("studentid");
			id = mStudentId;
		}
		showCardIdInfo(id);

	}

	/**
	 * 获取id卡信息
	 *
	 * @param id_card
	 */
	private void showCardIdInfo(String id_card) {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/director/carList";
		} else {
			url = BaseProtocol.HOST_BASE + "/director/carList";
		}
		RequestParams params = BaseProtocol.getRequestParams(url);
		params.addParameter("mytype", type);
		params.addParameter("myid", id_card);
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProGetIdCardInfo proGetIdCardInfo = new Gson().fromJson(result, ProGetIdCardInfo.class);
				if (proGetIdCardInfo != null && proGetIdCardInfo.getCode() == 0) {
					carListBeen = proGetIdCardInfo.getCarList();
					list.clear();
					for (int i = 0; i < carListBeen.size(); i++) {
						list.add(carListBeen.get(i));
					}
					mHandler.sendEmptyMessage(FINSH);
				}

			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});
	}

	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
		fill_add.setOnClickListener(this);
		lv_binding_id_card.setOnItemClickListener(this);
		lv_binding_id_card.setOnItemLongClickListener(this);

	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
			case R.id.fill_add:
				Intent intent = new Intent(this, EditBabyNameActivity.class);
				intent.putExtra("relative_iv_baby", "25");
				intent.putExtra("Id", id);
				intent.putExtra("myType", type);
				startActivityForResult(intent, Constant.REQUEST_CODE);
				break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent(this, EditBabyNameActivity.class);
		intent.putExtra("relative_iv_baby", "26");
		intent.putExtra("id", carListBeen.get(position).getId() + "");
		startActivityForResult(intent, Constant.REQUEST_CODE);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.i(TAG, "onActivityResult: resultCode = " + resultCode);
		Log.i(TAG, "onActivityResult: requestCode = " + requestCode);
		if (requestCode == Constant.REQUEST_CODE && resultCode == Constant.SETTING_ID_CARD_NUM_SAVE_SUCCESS) {
			distinguishType();
		}
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
		final Dialog dialog = new Dialog(this, R.style.dialog);
		View contentView = LayoutInflater.from(this).inflate(
				R.layout.dialog_exit, null);
		dialog.setContentView(contentView);
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
		TextView textView1990 = (TextView) contentView.findViewById(R.id.textView1990);
		textView1990.setText("确定删除当前ID卡吗?");
		TextView ok = (TextView) contentView.findViewById(R.id.button_ok);
		TextView cacel = (TextView) contentView.findViewById(R.id.button_cacel);
		ok.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				showDeleteIdCard(position);
			}
		});
		cacel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		return true;

	}

	/**
	 * 删除绑定的id卡
	 *
	 * @param position
	 */
	private void showDeleteIdCard(int position) {
		list.remove(position);
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/director/carChange";
		} else {
			url = BaseProtocol.HOST_BASE + "/director/carChange";
		}
		RequestParams params = BaseProtocol.getRequestParams(url);
		params.addParameter("type", "3");
		params.addParameter("myid", id);
		params.addParameter("mytype", type);
		params.addParameter("cid", carListBeen.get(position).getId());
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProShowClickGood proGetIdCardInfo = new Gson().fromJson(result, ProShowClickGood.class);
				if (proGetIdCardInfo != null && proGetIdCardInfo.getCode() == 0) {
					Toast.makeText(BindingIdCardActivity.this, getString(R.string.delete_id_success), Toast.LENGTH_SHORT).show();
					showCardIdInfo(id);
				} else if (proGetIdCardInfo.getCode() == 1) {
					Toast.makeText(BindingIdCardActivity.this, getString(R.string.id_card_already_exist), Toast.LENGTH_SHORT).show();
				} else if (proGetIdCardInfo.getCode() == -1)
					Toast.makeText(BindingIdCardActivity.this, getString(R.string.delete_id_fail), Toast.LENGTH_SHORT).show();

			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});
	}
}
